import { EventTarget } from "cc";
import { NativeInitDataDto } from "../native/nativeDataModels/NativeInitDataDto";
/**存储和游戏功能无关的数据*/
export class GlobelDataMgr {
    private static _instance:GlobelDataMgr;
    private _eventTarget: EventTarget = new EventTarget();
    private _token:string;
    private _nativeData:NativeInitDataDto;
    private _sceneName:SceneName;//当前场景
    private _networkDelay: number; //网络延迟单位毫秒
    /**本地时间和服务器的时间戳差*/
    private _timeStampDiffer:number=0;
    public static get instance():GlobelDataMgr{
        if(this._instance==null)
        {
            this._instance = new GlobelDataMgr();
        }
        return this._instance;
    }
    public setToken(token:string):void{
        this._token = token;
    }
    public getToken():string{
        return this._token;
    }
    /**设置服务器时间戳*/
    public setSocketTimeStamp(socketTimeStamp:number):void{
        this._timeStampDiffer = socketTimeStamp-Date.now()
        this.dispatchEvent(GlobelDataEventType.TIME_STAMP_DIFFER);
    }
    /**获取当前时间戳 已经计算过服务器差值*/
    public getDate():number{
        return Date.now()+this._timeStampDiffer;
    }
    /**设置当前网络延迟*/
    public setNetworkDelay(networkDelay: number): void {
        if (this._networkDelay != networkDelay) {
            this._networkDelay = networkDelay;
            this.dispatchEvent(GlobelDataEventType.NETWORK_DELAY);
        }
    }
    public setNativeData(data:NativeInitDataDto):void{
        this._nativeData = data;
    }
    public setSceneName(sceneName:SceneName):void{
        this._sceneName = sceneName;
    }
    /**当前场景name*/
    public getSceneName():SceneName{
        return this._sceneName;
    }
    
    /**监听数据变化*/
    public on(type: string, callback: any, thisArg?: any, once?: boolean): void {
        this._eventTarget.on(type, callback, thisArg, once);
    }
    /**移除监听*/
    public off(type: string, callback: any, thisArg?: any): void {
        this._eventTarget.off(type, callback, thisArg);
    }
    private dispatchEvent(type:GlobelDataEventType,arg0?: any): void {
        this._eventTarget.emit(type,arg0);
    }
}
export enum GlobelDataEventType{
    NETWORK_DELAY = 'networkDelay',//网络延迟
    TIME_STAMP_DIFFER = 'time_stamp_differ'//时间戳更新
}
export enum EnvironmentType{
    production = 1,//正式服
    test = 2,//测试服
}
export enum SceneName{
    main = "Main",//入口
    hall = "Hall",//大厅
}
